//
//  LocationTableViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/4/7.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "LocationTableViewController.h"

@interface LocationTableViewController ()

@property (nonatomic, copy)NSArray *array;
@property (nonatomic, assign)NSInteger selectIndex;

@end

@implementation LocationTableViewController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND_COLOR;
    self.navigationItem.title = @"选择位置";
    _array = @[@"不显示位置",@"杭州市",@"浙江工商大学图书馆",@"浙江工商大学现教中心",@"浙江工商大学下沙校区",@"浙江工商大学钱江湾生活园区",@"浙江工商大学A教学楼",@"浙江工商大学综合楼",@"墨湖",@"下沙宝龙广场",@"浙江工商大学下沙食堂",@"横店电影城(下沙店)",@"仁和社区"];
    _selectIndex = 0;
    //添加保存按钮
    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveLocation)];
    self.navigationItem.rightBarButtonItem = saveBtn;
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = _array[indexPath.row];
    cell.backgroundColor = BACKGROUND_COLOR;
    if (indexPath.row == 0) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 删除选中的效果
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    // 取消前一个选中的
    NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_selectIndex inSection:0];
    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
    lastCell.accessoryType = UITableViewCellAccessoryNone;
    // 保存刚选中的索引
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    _selectIndex = indexPath.row;
}

#pragma mark - *******私有方法*******
- (void)saveLocation{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"locationNotification" object:_array[_selectIndex]];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
